package 结构性模式


/**
 * 动态地给一个对象添加一些额外的职责。就增加功能来说，相比生成子类更为灵活
 */

interface TextNode2 {
    fun setText(text: String)
    fun getText(): String
}

class SpanNode(private var text: String) : TextNode2 {
    override fun setText(text: String) {
        this.text = text
    }

    override fun getText(): String {
        return "<span>$text</span>"
    }

}

abstract class NodeDecorator(private var target: TextNode2) : TextNode2 {
    override fun setText(text: String) {
        this.target.setText(text)
    }
}

class BoldDecorator(private var target: TextNode2) : NodeDecorator(target) {
    override fun getText(): String {
        return "<b>" + this.target.getText() + "</b>";
    }

}

fun main() {
    BoldDecorator(SpanNode("1234")).also { println(it.getText()) }
}